﻿package tv.colmeia {
	/**
	* ...
	* @author Martin Fabichak @ colmeia.tv
	*/
	
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import tv.colmeia.comm.GameSocket;
	import tv.colmeia.events.GameSocketEvent;
	
	import caurina.transitions.Tweener;	
	
	public class Slide extends Sprite {
		private const DEF_ESPACAMENTO:int = 250;
		
		public var slide1_mc:MovieClip;
		public var slide2_mc:MovieClip;
		private var _socket:GameSocket;
		
		public function Slide() {
			slide1_mc.gotoAndStop(1); slide2_mc.gotoAndStop(1);
			
			slide1_mc.alpha = 0;
			Tweener.addTween(slide1_mc, { alpha:1, time:1, transition:"linear" } );
			
			_socket = new GameSocket("192.168.2.71", 8484);
			_socket.addEventListener(GameSocketEvent.GAME_EVENT, key);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyTeclado);
		}
		
		public function goRight():void {
			var frame:int = slide2_mc.currentFrame + 1;
			if (frame > slide2_mc.totalFrames) frame = 1;
			slide2_mc.gotoAndStop(frame);
			slide2_mc.x = slide2_mc.width + DEF_ESPACAMENTO;
			Tweener.addTween(slide1_mc, { x:-slide1_mc.width - DEF_ESPACAMENTO, time:1, transition:"easeInBack" } )
			Tweener.addTween(slide2_mc, { x:0, time:1, transition:"easeInBack", onComplete:function () {
				slide1_mc.gotoAndStop(frame);
				slide1_mc.x = 0;
				slide2_mc.x = slide1_mc.width + DEF_ESPACAMENTO;
			} } )
		}
		
		public function goLeft():void {
			var frame:int = slide2_mc.currentFrame - 1;
			if (frame == 0 ) frame = slide2_mc.totalFrames;
			slide2_mc.gotoAndStop(frame);
			slide2_mc.x = - slide2_mc.width - DEF_ESPACAMENTO;
			Tweener.addTween(slide1_mc, { x:slide1_mc.width + DEF_ESPACAMENTO, time:1, transition:"easeInBack" } )
			Tweener.addTween(slide2_mc, { x:0, time:1, transition:"easeInBack", onComplete:function () {
				slide1_mc.gotoAndStop(frame);
				slide1_mc.x = 0;
				slide2_mc.x = slide1_mc.width + DEF_ESPACAMENTO;
			} } )
		}
		private function keyTeclado(e:KeyboardEvent):void {
			if(Tweener.isTweening(slide1_mc)) return;
			if (e.keyCode == 39) {
				goRight();
			}else if (e.keyCode == 37) {
				goLeft();
			}
		}
		private function key(e:GameSocketEvent):void {
			if(Tweener.isTweening(slide1_mc)) return;
			trace( "e.key : " + e.key );
			if (e.key == 39) {
				goRight();
			}else if (e.key == 37) {
				goLeft();
			}
		}
	}
	
}